package TAIC.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.channels.FileChannel;
import java.util.Scanner;
import java.util.Vector;

public class GetODP {

	static final int ODPTERM = 1500000;

	static public void main(String str[]) throws Exception {
		if ( str.length != 1 ) {
			System.out.println ( "please input the folder names file" ) ;
			return ;
		}
		
		
		Vector<String> vocName[] = new Vector [300];
		String path[] = new String[ODPTERM];
		String odpName[] = new String[ODPTERM];
		String temp = null;
		int docs[] = new int[300];
		Scanner scanner = new Scanner(new File( str[ 0 ] ));
		int VOCLen = 0;
		while (scanner.hasNext()) {
			temp = scanner.next().trim().replace('-', '.').toLowerCase()+ "." ;
			int pos =  temp.indexOf('.');
			vocName[ VOCLen ] = new Vector < String > () ;
			vocName[ VOCLen ].add( temp.substring(0, pos )); 
			while ( pos < temp.length() -1  ) {
				int nextPos = temp.indexOf( '.' , pos + 1 ); 
				String temp1 ; 
				if ( nextPos < 0 )  temp1 = temp.substring(pos + 1);
				else temp1= temp.substring(pos + 1 , nextPos );
				if ( temp1.length() <= 3 ) temp1 += "_" ;
				vocName[VOCLen].add( temp1 ) ; 
				pos = nextPos ; 
			}
			VOCLen ++ ; 
		}

		//		System.out.println( vocName[ 3 ].get(0) + " " + vocName[ 3 ].get(1) + " " + vocName[ 3 ].get(2));
		//			System.exit( 0 );
		scanner.close();
		scanner = new Scanner(new File("LIST"));

		int ODPLen = 0;
		while (scanner.hasNextLine()) {
			temp = scanner.nextLine().trim().toLowerCase();
			int pos = temp.indexOf('\t');
			if (pos == -1) continue;
			path[ODPLen] = temp.substring(0, pos);
			odpName[ODPLen] = "_" + temp.substring(temp.lastIndexOf('\t') + 1, temp.length()).replace('/', '_') + "_" ;
			ODPLen++;
		}
		//			System.out.println ( path [ 0 ]+ " " + odpName [ 0 ]) ; 
		//			System.out.println ( path [ 10000 ]+ " " + odpName [ 10000 ]) ; 
		//			if ( true ) return ; 
		// System.out.println(VOCLen + " " + ODPLen

		for (int i = 0; i < VOCLen; i++) {
			File tempFile = new File(vocName[i].get(0) + "\\");
			if (!tempFile.exists()) tempFile.mkdirs();
			String configname = vocName[i].get(0) + "\\index.txt";
			PrintStream configFile = new PrintStream(new File(configname));
			PrintStream nameFile = new PrintStream ( new File ( vocName[i].get(0)+ "\\cateName.txt")) ;
			for (int j = 0; j < ODPLen; j++) {
				if ( compare ( odpName [j], vocName[ i ])) 
					if ( copyFile(new File(
							"\\\\ajax\\users\\arthur\\ODP\\[1.2 Text]\\"   //	"\\\\poseidon\\Team\\WebSearch\\ODP\\[1.2 Text]\\"							
							+ path[j] + ".htm.txt"), new File( vocName[i].get(0) + "\\" + docs[i] + ".txt"))) {
						configFile.println(docs[i] + ".txt");
						nameFile.println ( odpName [ j ] ) ;
						docs[i]++;
					} 
			}
			configFile.close();
		}			
	}

	public static boolean compare ( String str , Vector < String > words ) {
		int pos ; 
		
		for ( int i = 1 ; i < words.size() ; i++ ) {
			pos = str.indexOf(words.get(i));
			if (  pos >= 0
					//&& str.charAt(pos + words.get(i).length() ) == '_' && str.charAt(pos - 1) == '_' 
					) return true ;
					//&& ((pos + words.get(i).length() >= str.length() || 
						//	str.charAt(pos + words.get(i).length()) == '_' ))) 
		}
		return false;
	}
		
	public static boolean copyFile(File in, File out) {
		boolean done = true;
		FileChannel inChannel = null;
		FileChannel outChannel = null;

		try {

			inChannel = new FileInputStream(in).getChannel();
			outChannel = new FileOutputStream(out).getChannel();
			inChannel.transferTo(0, inChannel.size(), outChannel);
		}
		catch (IOException e) {
			e.printStackTrace();
			done = false;
		}
		try {
			if (inChannel != null) inChannel.close();
			if (outChannel != null) outChannel.close();

		}
		catch (Exception e) {
		}
		return done;
	}
}
